#include "Storage.h"
#include "MainLogic.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

Storage :: Storage()
{}

void Storage :: readFromFile()
{
	int index;
	stringstream str;
	ifstream textFile("Tempus.txt");
	string temp = "";
	string id, name, priority, time, location;

	if(textFile.is_open())
	{
		while(!textFile.eof())
		{
			getline(textFile,temp);
			str << temp;
			str >> id >> name >> priority >> time >> location;
			index = dateDifference(id);

			currentList[index].addEvent(id, name, priority, time, location);
		}
	}
	else cout << "Error in opening file!" << endl;
}

void Storage :: writeToFile()
{
	ofstream textFile("Tempus.txt");
	
	for (int i=0; i < currentList.size(); i++){
		for (int j=0; j < currentList[i].size(); j++){
			stringstream details;
			details << currentList[i][j].id << " " << currentList[i][j].name << " ";
			details << currentList[i][j].priority << " " << currentList[i][j].time << " ";
			details << currentList[i][j].location << endl;
			textFile << details.str();
		}
	}

	textFile.close();
}

